/**
 *
 */
package ua.at.tsvetkov.samizdat.activities;

import java.io.File;

import ua.at.tsvetkov.samizdat.Const;
import ua.at.tsvetkov.samizdat.adapters.AdtLoadedBooks;
import ua.at.tsvetkov.samizdat.data.Book;
import ua.at.tsvetkov.samizdat.data.DataLoadedAuthorsBooks;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

/**
 * @author A.Tsvetkov 2011, http://tsvetkov.at.ua, al@ukr.net
 */
public class AcLoadedAuthorBooks extends AcTemplate {

	private String							path;
	private DataLoadedAuthorsBooks	dataLoadedAuthorsBooks;
	private AdtLoadedBooks				adtLoadedBooks;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		path = Const.PATH_BOOKS + getIntent().getExtras().getString("author") + File.separator;
		dataLoadedAuthorsBooks = new DataLoadedAuthorsBooks(path);
		dataLoadedAuthorsBooks.load();
		adtLoadedBooks = new AdtLoadedBooks(this, dataLoadedAuthorsBooks);
		addListView();
		listView.setAdapter(adtLoadedBooks);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Book book = dataLoadedAuthorsBooks.getBookData(position);
				book.openAcBook(AcLoadedAuthorBooks.this);
			}

		});
		listView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View view, int position, long id) {
				deleteBook(position, view);
				return false;
			}

		});
	}

	private void deleteBook(int position, View view) {
		// AlertDialog.Builder builder = new AlertDialog.Builder(this);
		//
		// builder.setTitle(dataLoadedAuthorsBooks.booksData[position].);
		// builder.setPositiveButton("Удалить", new OnClickListener() {
		//
		// @Override
		// public void onClick(DialogInterface dialog, int which) {
		// dataLoadedAuthors.deleteAutor(position);
		// dataLoadedAuthors.load();
		// runOnUiThread(new Runnable() {
		//
		// @Override
		// public void run() {
		// adtLoadedAuthors.notifyDataSetChanged();
		//
		// }
		// });
		// }
		//
		// });
		// builder.setNegativeButton("Оставить", null);
		// builder.show();

	}

	/*
	 * (non-Javadoc)
	 * @see ua.at.tsvetkov.debug.AcSimpleTrackingActivity#onResume()
	 */
	@Override
	protected void onResume() {
		adtLoadedBooks.refresh();
		if (adtLoadedBooks.getCount() == 0)
			finish();
		adtLoadedBooks.notifyDataSetChanged();
		super.onResume();
	}

}
